#include <stdio.h>
#include <algorithm>

#include <queue>
using namespace std;

int n;
int quals[1010];
int pai[1010];
int m;

int c;
int last;
int cost;

class app {
	public:
	int a, b, c;
};

app  appl[10010];

bool sortWay(app a, app b) {
	if (a.b == b.b) 
		return a.c<b.c;

	return a.b<b.b;
}

int getroot(int x) {
	while (x != pai[x]) {
		x=pai[x];
	}

	return pai[x];
}

bool unio(int a, int b) {
	a = getroot(a);
	b = getroot(b);

	if (a == b) return false;

	return true;
}

priority_queue< pair<int, int> > pQueue;
int edges;

bool super[1010];

int main() {
	scanf("%d", &n);

	for (int i=0; i<n; i++) 
		scanf("%d", &quals[i]);

	scanf("%d", &m);

	for (int i=0; i<m; i++) {
		scanf("%d %d %d", &appl[i].a, &appl[i].b, &appl[i].c);
		appl[i].a--;
		appl[i].b--;
	}

	sort(appl, appl+m, sortWay);

	c=0;
	cost=0;
	edges=0;

	for (int i=0; i<m; i++) {
		pQueue.push(make_pair(-appl[i].c, i));
	}

	for (int i=0; i<n; i++) {
		super[i]=false;
	}

	for (int i=0; i<n; i++) {
		pai[i]=i;
	}

	int ind, u, v;

	while (edges < (n-1) && !pQueue.empty()) {
		ind = pQueue.top().second;
		pQueue.pop();
		u = appl[ind].a;
		v = appl[ind].b; 

		if (super[v])
			continue;

		if (unio(u, v)) {
			pai[v] = u;
			cost+=appl[ind].c;
			edges++;
			super[v]=true;
		}
	}

	for (int i=0; i<n; i++) {
		if (!super[i])
			c++;
	}

	if (c>1) 
		printf("-1\n");
	else
		printf("%d\n", cost);

	return 0;
}
